<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-col-md5">
            <div class="layui-card">
                <div class="layui-card-body layui-row layui-col-space10">
                    <form class="layui-form">
                        <div class="layui-form-item">
                            <label class="layui-form-label">任务名：</label>
                            <div class="layui-input-block">
                                <input lay-verify="required" type="text" name="taskName" placeholder="请输入任务名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">源：</label>
                            <div class="layui-input-block">
                                <select id="source_id" name="sourceId" lay-verify="required">
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">源库：</label>
                            <div class="layui-input-block">
                                <input type="text" id="source_db" name="sourceDb" placeholder="请输入数据库名" autocomplete="off" class="layui-input" lay-verify="required">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">目标：</label>
                            <div class="layui-input-block">
                                <select id="target_id" name="targetId" lay-verify="required">
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">目标库：</label>
                            <div class="layui-input-block">
                                <input type="text" name="targetDb" placeholder="请输入目标数据库名" autocomplete="off" class="layui-input" lay-verify="required">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">异常处理：</label>
                            <div class="layui-input-block">
                                <select name="error_break" lay-verify="required">
                                    <option value="1">继续执行</option>
                                    <option value="0">停止执行</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item model-form-footer">
                            <button class="layui-btn layui-btn-primary" type="button" id="btn-cancel">取消</button>
                            <button class="layui-btn" lay-filter="task-form-submit" lay-submit>保存</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="layui-col-md7">
            <div class="layui-card">
                <div class="layui-card-body layui-row layui-col-space10">
                    <table class="layui-table" id="task-tables"></table>
                </div>
            </div>
        </div>
        <div class="layui-clear"></div>
    </div>
</div>

<script>
    layui.use(['form', 'table','config', 'crown'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var config = layui.config;
        var layer = layui.layer;
        var crown = layui.crown;
        var table = layui.table;

        crown.get('/db/connections',{pageSize:999}, function(data){
            var items = data.items;
            items.forEach(function (x) {
                $('#source_id').append(new Option(x.name, x.id));
                $('#target_id').append(new Option(x.name, x.id));
            });
            form.render();
        });

        var sourceTable = table.render({
            elem: '#task-tables',
            height: 380,
            parseData: config.parseData,
            response: config.response,
            cols: [[
                {checkbox: true},
                {field: 'tableName', align: 'center', sort: true, title: '表名'},
                {field: 'tableSize', align: 'center', sort: true, title: '预估大小(MB)'},
                {field: 'rows', align: 'center', sort: true, title: '行数'}
            ]]
        });

        $('#source_db').blur(function(){
            var database = $(this).val();
            var sourceId = $('#source_id').val();
            if(sourceId && database){
                sourceTable.reload({
                    url: '/task/tables?sourceId='+sourceId+'&database='+database
                });
            }
        });

        $('#btn-cancel').click(function(){
            crown.closePopupCenter();
        });

        form.on('submit(task-form-submit)', function (data) {
            layer.load(2);
            var form_data = data.field;
            var checkStatus = table.checkStatus('task-tables');
            var selectTables = checkStatus.data;
            var tables = [];
            selectTables.forEach(function(item){
                tables.push(item.tableName);
            });
            if(tables.length === 0){
                layer.closeAll('loading');
                layer.msg("请选择需要迁移的数据库表!", {icon: 2});
                return false;
            }
            form_data['tables'] = tables;
            crown.post('/task', form_data, function(r){
                layer.closeAll('loading');
                layer.msg(r.msg, {icon: 1});
                crown.finishPopupCenter();
            });
            return false;
        });

    });
</script>